﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmHoGa : Form
    {
        public FrmHoGa()
        {
            InitializeComponent();
        }

        private void FrmHoGa_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmHoGa_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(689, 622);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrHoga prHoga = new PrHoga();
            prHoga.Show();
            this.Hide();
        }

        private void FrmHoGa_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
        // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            Values = TxtCaoHg .Text;//0
            Values = Values +"\t"+ TxtDayMongN .Text;//1
            Values = Values +"\t"+ TxtDayMongT.Text;//2
            Values = Values +"\t"+ TxtDayThanhHg .Text;//3
            Values = Values +"\t"+ txtDaydanH .Text;//4
            Values = Values +"\t"+ TxtMorongMong.Text;//5
            Values = Values +"\t"+ TxtNgangHg .Text;//6
            Values = Values +"\t"+ TxtRongGo .Text;//7
            Values = Values +"\t"+ TxtRongHg .Text;//8
            Values = Values +"\t"+ txtDemmong .Text;//9
            Values = Values +"\t"+ TxtThCl .Text;//10
            Values = Values +"\t"+ TxtThCt .Text;//11
            Values = Values +"\t"+ txtSoDan .Text;//12
            regKey.SetValue("KtHoga", Values);
            regKey.Close();
        }
        // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtHoga").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                TxtCaoHg.Text = STR[i++];//0
                TxtDayMongN.Text = STR[i++];//1
                TxtDayMongT.Text = STR[i++];//2
                TxtDayThanhHg.Text = STR[i++];//3
                txtDaydanH.Text = STR[i++];//4
                TxtMorongMong.Text = STR[i++];//5
                TxtNgangHg.Text = STR[i++];//6
                TxtRongGo.Text = STR[i++];//7
                TxtRongHg.Text = STR[i++];//8
                txtDemmong.Text = STR[i++];//9
                TxtThCl.Text = STR[i++];//10
                TxtThCt.Text = STR[i++];//11
                txtSoDan.Text = STR[i++];//12
                regKey.Close();
            }
        }

         // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.hg)|*.hg";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //HO GA
                Str = "------HO GA------"+"\r\n";
                Str = Str + "Cao ho ga H:" +"\t"+ TxtCaoHg.Text +"\r\n";
                Str = Str + "Day mong trong H1:" +"\t"+ TxtDayMongT.Text +"\r\n";
                Str = Str + "Day mong ngoai H2:" +"\t"+ TxtDayMongN.Text +"\r\n";
                Str = Str + "Day thanh B1:" +"\t"+ TxtDayThanhHg.Text +"\r\n";
                Str = Str + "Day dem mong:" +"\t"+ txtDemmong.Text +"\r\n";
                Str = Str + "Rong ho ga B:" +"\t"+ TxtRongHg.Text +"\r\n";
                Str = Str + "Rong ngang ho ga Bn:" +"\t"+ TxtNgangHg.Text +"\r\n";
                Str = Str + "Rong go b1:" +"\t"+ TxtRongGo.Text +"\r\n";
                Str = Str + "Mo rong b2:" +"\t"+ TxtMorongMong.Text +"\r\n";
                Str = Str + "Doc thanh ho ga i:" +"\t"+ CmbDocHg.Text +"\r\n";
                //Dan ho ga
                Str = Str + "------DAN HO GA------" +"\r\n";
                Str = Str + "So tam dan:" +"\t"+ txtSoDan.Text +"\r\n";
                Str = Str + "Day dan h:" +"\t"+ txtDaydanH.Text +"\r\n";
                Str = Str + "Chieu dai dan L:" +"\t"+ txtDaidanL.Text +"\r\n";
                Str = Str + "Rong dan W:" +"\t"+ txtRongdanB.Text +"\r\n";
                Str = Str + "Be tong bao ve:" +"\t"+ txtBtBv.Text +"\r\n";
                Str = Str + "Duong kinh thep d1:" +"\t"+ TxtThCl.Text +"\r\n";
                Str = Str + "Cach khoang thep d1:" +"\t"+ TxtCkCl.Text +"\r\n";
                Str = Str + "Duong kinh thep d2:" +"\t"+ TxtThCt.Text +"\r\n";
                Str = Str + "Cach khoang thep d2:" +"\t"+ TxtCkCt.Text +"\r\n";
                Str = Str + "Thep goc quanh dan:" +"\t"+ CmbThgHg.Text +"\r\n";
                //Khoet lo mat truoc
                Str = Str + "------KHOET LO MAT TRUOC------" +"\r\n";
                Str = Str + "Khong khoet lo:" +"\t"+ OptMTrK.Checked.ToString() +"\r\n";
                Str = Str + "Khoet lo dang hop:" +"\t"+ OptMTrH.Checked.ToString() +"\r\n";
                Str = Str + "Khoet lo dang tron:" + "\t" + OptMTrTr.Checked.ToString() +"\r\n";
                Str = Str + "Cach day h:" +"\t"+ TxtMTrCd.Text +"\r\n";
                Str = Str + "Goc voi phuong OY:" +"\t"+ TxtMSY.Text +"\r\n";
                Str = Str + "Rong lo B:" +"\t"+ TxtMTrB.Text +"\r\n";
                Str = Str + "Cao lo H:" +"\t"+ TxtMTrH.Text +"\r\n";
                Str = Str + "Duong kinh lo:" +"\t"+ CmbMTr.Text +"\r\n";
                //Khoet lo mat sau
                Str = Str + "------KHOET LO MAT SAU------" +"\r\n";
                Str = Str + "Khong khoet lo:" +"\t"+ OptMSK.Checked.ToString() +"\r\n";
                Str = Str + "Khoet lo dang hop:" +"\t"+ OptMSH.Checked.ToString() +"\r\n";
                Str = Str + "Khoet lo dang tron:" +"\t"+ OptMSTr.Checked.ToString() +"\r\n";
                Str = Str + "Cach day h:" +"\t"+ TxtMSCd.Text +"\r\n";
                Str = Str + "Goc voi phuong OY:" +"\t"+ TxtMSY.Text +"\r\n";
                Str = Str + "Rong lo B:" +"\t"+ TxtMSB.Text +"\r\n";
                Str = Str + "Cao lo H:" +"\t"+ TxtMSH.Text +"\r\n";
                Str = Str + "Duong kinh lo:" +"\t"+ CmbMS.Text +"\r\n";

                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
        // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.hg)|*.hg";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //HO GA
                TxtCaoHg.Text=Values[i].Split('\t')[1]; i++;
                TxtDayMongT.Text=Values[i].Split('\t')[1]; i++;
                TxtDayMongN .Text=Values[i].Split('\t')[1]; i++;
                TxtDayThanhHg .Text=Values[i].Split('\t')[1]; i++;
                txtDemmong .Text=Values[i].Split('\t')[1]; i++;
                TxtRongHg .Text=Values[i].Split('\t')[1]; i++;
                TxtNgangHg .Text=Values[i].Split('\t')[1]; i++;
                TxtRongGo .Text=Values[i].Split('\t')[1]; i++;
                TxtMorongMong .Text=Values[i].Split('\t')[1]; i++;
                CmbDocHg .Text=Values[i].Split('\t')[1]; i++;
                //Dan ho ga
                i = i + 1;
                txtSoDan .Text=Values[i].Split('\t')[1]; i++;
                txtDaydanH .Text=Values[i].Split('\t')[1]; i++;
                txtDaidanL .Text=Values[i].Split('\t')[1]; i++;
                txtRongdanB .Text=Values[i].Split('\t')[1]; i++;
                txtBtBv .Text=Values[i].Split('\t')[1]; i++;
                TxtThCl .Text=Values[i].Split('\t')[1]; i++;
                TxtCkCl .Text=Values[i].Split('\t')[1]; i++;
                TxtThCt .Text=Values[i].Split('\t')[1]; i++;
                TxtCkCt .Text=Values[i].Split('\t')[1]; i++;
                CmbThgHg .Text=Values[i].Split('\t')[1]; i++;
                //Khoet lo mat truoc
                i = i + 1;
                OptMTrK.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                OptMTrH.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                OptMTrTr.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                TxtMTrCd .Text=Values[i].Split('\t')[1]; i++;
                TxtMTrY .Text=Values[i].Split('\t')[1]; i++;
                TxtMTrB .Text=Values[i].Split('\t')[1]; i++;
                TxtMTrH .Text=Values[i].Split('\t')[1]; i++;
                CmbMTr .Text=Values[i].Split('\t')[1]; i++;
                //Khoet lo mat sau
                i = i + 1;
                OptMSK.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                OptMSH.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                OptMSTr.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                TxtMSCd .Text=Values[i].Split('\t')[1]; i++;
                TxtMSY .Text=Values[i].Split('\t')[1]; i++;
                TxtMSB .Text=Values[i].Split('\t')[1]; i++;
                TxtMSH .Text=Values[i].Split('\t')[1]; i++;
                CmbMS.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }
        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmHoGa_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
